package com.huawei.crm.web.domain;

import com.alibaba.fastjson.JSON;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;

/**
 * 单条数据结果集
 *
 * @param <T>
 * @author luohua
 * @date 2021年12月30日17:20:05
 */
@Data
@NoArgsConstructor
@EqualsAndHashCode(callSuper = true)
public class SingleResult<T> extends BaseResult {

    private static final long serialVersionUID = 5611250944096363271L;

    /**
     * 响应数据结果
     */
    private T data;

    public SingleResult(T data) {
        super(ResultCode.SUCCESS);
        this.data = data;
    }

    public SingleResult(int code, String message) {
        super(code, message);
    }

    public SingleResult(int code, String message, T data) {
        super(code, message);
        this.data = data;
    }

    public SingleResult<T> code(Integer code) {
        this.setCode(code);
        return this;
    }

    public SingleResult<T> message(String message) {
        this.setMessage(message);
        return this;
    }

    public SingleResult<T> data(T data) {
        this.setData(data);
        return this;
    }

    public static <T> SingleResult<T> of(ResultCode status) {
        return new SingleResult<>(status.code(), status.message());
    }

    public static <T> SingleResult<T> of(ResultCode status, String message) {
        return new SingleResult<>(status.code(), message);
    }

    public static <T> SingleResult<T> of(ResultCode status, T data) {
        return new SingleResult<>(status.code(), status.message(), data);
    }

    public static <T> SingleResult<T> of(ResultCode status, String message, T data) {
        return new SingleResult<>(status.code(), message, data);
    }

    public static <T> SingleResult<T> success() {
        return new SingleResult<>(ResultCode.SUCCESS.code(), ResultCode.SUCCESS.message(), null);
    }

    public static <T> SingleResult<T> success(T data) {
        return new SingleResult<>(ResultCode.SUCCESS.code(), ResultCode.SUCCESS.message(), data);
    }

    public static <T> SingleResult<T> error() {
        return new SingleResult<>(ResultCode.FAILED.code(), ResultCode.FAILED.message());
    }

    public static <T> SingleResult<T> error(String message) {
        return new SingleResult<>(ResultCode.FAILED.code(), message);
    }

    public static <T> SingleResult<T> error(int code, String message) {
        return new SingleResult<>(code, message);
    }

    @Override
    public String toString() {
        return JSON.toJSONString(this);
    }

}
